package com.lanou.service.impl;

import com.alibaba.fastjson.JSON;
import com.lanou.entity.TbItemCat;
import com.lanou.entity.TbItemCatExample;
import com.lanou.mapper.TbItemCatMapper;
import com.lanou.service.ItemCatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by QuChao on 2018/11/19
 */
@Service
public class ItemCatServiceImpl implements ItemCatService {
    @Autowired
    private TbItemCatMapper tbItemCatMapper;

    @Autowired
    private JedisPool jedisPool;

    /**
     * 将查询数据库得到的Map集合，转成JSON类型，转存到Redis中
     */
    @Override
    public void addInfo() {
        String jsonString = JSON.toJSONString(getAllCart());
        Jedis jedis = jedisPool.getResource();
        jedis.set("allCat", jsonString);
    }

    /**
     * 在数据库中获取分类信息的方法
     *
     * @return Map集合
     */
    private Map<String, Object> getAllCart() {
        TbItemCatExample t1 = new TbItemCatExample();
        t1.createCriteria().andParentIdEqualTo(0L);
        List<TbItemCat> oneTbItemCatList = tbItemCatMapper.selectByExample(t1);
        Map<String, Object> catMap = new HashMap<>();
        List<Map<String, Object>> oneList = getList(oneTbItemCatList);
        catMap.put("data", oneList);
        return catMap;
    }


    /**
     * 通过父类目获取子类目的方法
     * 如果子类目依旧是父类目的话，使用递归继续查询改子类目下的子类目
     *
     * @param tbItemCatList 父类目集合
     * @return 子类目集合
     */
    public List<Map<String, Object>> getList(List<TbItemCat> tbItemCatList) {
        List<Map<String, Object>> list = new ArrayList<>();
        for (TbItemCat tbItemCat : tbItemCatList) {
            Map<String, Object> map = new HashMap<>();
            TbItemCatExample tbItemCatExample = new TbItemCatExample();
            if (tbItemCat.getIsParent()) {
                tbItemCatExample.createCriteria().andParentIdEqualTo(tbItemCat.getId());
                List<TbItemCat> tbItemCats = tbItemCatMapper.selectByExample(tbItemCatExample);
                List<Map<String, Object>> list1 = getList(tbItemCats);
                map.put("id", tbItemCat.getId());
                map.put("name", tbItemCat.getName());
                map.put("parentId", tbItemCat.getParentId());
                map.put("childList", list1);
            } else {
                map.put("id", tbItemCat.getId());
                map.put("name", tbItemCat.getName());
            }
            list.add(map);
        }
        return list;
    }

    /**
     * 在Redis中查询类目集合，反序列成Map集合，返回
     *
     * @return Map集合
     */
    @Override
    public Map<String, Object> getInfo() {
        Jedis jedis = jedisPool.getResource();
        String allCat = jedis.get("allCat");
        Map<String, Object> parse = (Map<String, Object>) JSON.parse(allCat);
        jedis.close();
        return parse;
    }
}
